
#include <ssc_bsp.h>
#include <gpio.h>
#include "spi.h"
#include "stdio.h"
#include "el9800hw.h"


/**
 * select esc SPI  NSS pin reset
 */
void spi_Select(void)
{
	HAL_GPIO_WritePin(ESC_CS_GPIO_Port, ESC_CS_Pin, GPIO_PIN_RESET);
}

/**
 * deselect esc SPI  NSS pin set
 */
void spi_Deselect(void)
{
	HAL_GPIO_WritePin(ESC_CS_GPIO_Port, ESC_CS_Pin, GPIO_PIN_SET);
}


/**TIM_HandleTypeDef htim6; from <tim.c> */
extern TIM_HandleTypeDef htim6;
/**
 * Timer6 ESC-tim start IT mode
 */
void timer_Esc_IT_Start(void)
{
	HAL_TIM_Base_Start_IT(&htim6);
}

/**
 * Timer6 ESC-tim stop  IT mode
 */
void timer_Esc_IT_Stop(void)
{
	HAL_TIM_Base_Stop_IT(&htim6);
}


/**
 * ESC related PDI_IRQ, SYNC0, SYNC1 interrupt callback
 * and others interrupt callback can be add to here
 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if (htim->Instance == htim6.Instance)
	{
		TimerIsr();
		// printf(".\r\n");
	}
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	switch (GPIO_Pin)
	{
	case PDI_IRQ_Pin:
		/** from <el9800w.c> */
		EcatIsr();
//		printf("PDI\r\n");
		break;
	case SYNC0_Pin:
		/** from <el9800w.c> */
		Sync0Isr();
//		printf("SYNC0\r\n");
		break;
	case SYNC1_Pin:
		/** from <el9800w.c> */
		Sync1Isr();
//		printf("SYNC1\r\n");
		break;
	default:
		break;
	}
}

uint8_t WR_CMD(uint8_t cmd)
{
	uint8_t temp;

	HAL_SPI_TransmitReceive(&hspi1, &cmd, &temp, 1, 1000);
	
	return temp;
}
